package com.example.myapplication.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;

public class FindPswActivity extends AppCompatActivity {

    private TextView tv_title;
    private String suserid;
    private String ssex;
    private String snew_Psw;
    private String sconfirm_new_Psw;
    private String sphone;

    private EditText userid;

    private EditText new_Psw;
    private EditText confirm_new_Psw;
    private EditText phone;

    private Button change;

    private Button btn_back;
    private int count;        //用于计数失败次数

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        init();
    }
    private void init()
    {
        count = 0;
        userid = (EditText) findViewById(R.id.et_user_name);
        new_Psw = (EditText) findViewById(R.id.new_Psw);
        confirm_new_Psw = (EditText) findViewById(R.id.confirm_new_Psw);
        phone = (EditText) findViewById(R.id.et_phone);
        change = (Button) findViewById(R.id.btn_change);
        btn_back = (Button) findViewById(R.id.tv_back);
        tv_title = (TextView) findViewById(R.id.tv_main_title);
        tv_title.setText("忘记密码");

        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = findViewById(checkedId);
                String selectedOption = radioButton.getText().toString();
                // 根据选中的选项进行相应的处理
                if (selectedOption.equals("男")) {
                    // 处理选中男的情况
                    ssex = "男";
                } else if (selectedOption.equals("女")) {
                    // 处理选中女的情况
                    ssex = "女";
                } else if (selectedOption.equals("保密")) {
                    // 处理选中保密的情况
                    ssex = "保密";
                }
            }
        });

        //点击注册事件
        change.setOnClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View arg0){

                String regEx = "(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{6,20}$";
                Pattern pattern = Pattern.compile(regEx);
                getInfor();

                if(ssex == null) {
                    Toast.makeText(FindPswActivity.this, "请选择性别", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(suserid == null) {
                    Toast.makeText(FindPswActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(sphone == null) {
                    Toast.makeText(FindPswActivity.this, "请输入电话号码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(snew_Psw == null) {
                    Toast.makeText(FindPswActivity.this, "请输入新密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(sconfirm_new_Psw == null){
                    Toast.makeText(FindPswActivity.this, "请确认密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!sconfirm_new_Psw.equals(snew_Psw))
                {
                    Toast.makeText(FindPswActivity.this, "输入密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                Matcher matcher = pattern.matcher(snew_Psw);
                if(matcher.matches()){
                    OkHttpUtils
                            .post()
                            .url("http://121.36.92.128:8086/forgetPassword")
                            .addParams("username", suserid)
                            .addParams("sex", ssex)
                            .addParams("phone",sphone)
                            .addParams("password",snew_Psw)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int i) {
                                    System.out.println(e.toString());
                                }

                                @Override
                                public void onResponse(String response, int i) {
                                    if(count == 3){
                                        Toast.makeText(FindPswActivity.this, "失败次数超过三次,请稍后重试", Toast.LENGTH_SHORT).show();
                                        FindPswActivity.this.finish();
                                    } else if(response.equals("0")){
                                        Toast.makeText(FindPswActivity.this, "用户不存在", Toast.LENGTH_SHORT).show();
                                        count++;
                                    }else if(response.equals("-1")){
                                        Toast.makeText(FindPswActivity.this, "性别不正确", Toast.LENGTH_SHORT).show();
                                        count++;
                                    }else if(response.equals("-2")){
                                        Toast.makeText(FindPswActivity.this, "电话号码不正确", Toast.LENGTH_SHORT).show();
                                        count++;
                                    } else if (response.equals("-3")) {
                                        Toast.makeText(FindPswActivity.this, "密码不相同", Toast.LENGTH_SHORT).show();
                                        count++;
                                    } else{
                                        Toast.makeText(FindPswActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                        count = 0;
                                        FindPswActivity.this.finish();
                                    }
                                }
                            });
                }else{
                    Toast.makeText(FindPswActivity.this, "密码必须同时包含数字与字母,且长度为 6-20位", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //返回按钮的点击事件
        btn_back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                FindPswActivity.this.finish();
            }
        });
    }

    private void getInfor()
    {
        suserid = userid.getText().toString().trim();
        sphone = phone.getText().toString().trim();
        snew_Psw = new_Psw.getText().toString().trim();
        sconfirm_new_Psw = confirm_new_Psw.getText().toString().trim();
    }
}